/*
	cross_typedef.h
*/
#ifndef _CORSS_TYPEDEF_H
#define _CORSS_TYPEDEF_H

#include "transfer_size.h"

//Short data typedef
#ifndef _COMMON_H
typedef unsigned char      U8;
typedef unsigned short     U16;
typedef unsigned int       U32;
typedef unsigned long long U64;
typedef char               S8;
typedef short              S16;
typedef int                S32;
typedef long long          S64;

#endif

typedef	unsigned char	   uchar_t;
#include <stdint.h>
#ifdef _WIN32

/* bsd */
// #ifndef _STDINT
// #define _STDINT
// typedef unsigned char			u_char;
// typedef unsigned short			u_short;
// typedef unsigned int			u_int;
// typedef unsigned long			u_long;
// 
// /* sysv */
// typedef unsigned char			uchar;
// typedef unsigned short			ushort;
// typedef unsigned int			uint;
// typedef unsigned long			ulong;
// 
// typedef		unsigned char		u_int8_t;
// typedef		char				int8_t;
// typedef		unsigned short		u_int16_t;
// typedef		short				int16_t;
// typedef		unsigned int		u_int32_t;
// typedef		int					int32_t;
// 
// typedef		unsigned char		uint8_t;
// typedef		unsigned short		uint16_t;
// typedef		unsigned int		uint32_t;
// 
// typedef		unsigned long long	uint64_t;
// typedef		unsigned long long	u_int64_t;
// typedef		long long			int64_t;
// 
// typedef		unsigned char		uchar_t;
// typedef		unsigned long long __le64;
// #endif

#define TRACE_ERR printf
#define TRACE_DBG printf
#define TRACE_CBS printf
#define SLEEP(ms) Sleep(ms);
#define TIME_CNT_CURRENT(start_tm) 	start_tm = GetTickCount()
#define TIME_DIFF_MS(start_tm,stop_tm) (stop_tm - start_tm)
#define TIME_DIFF_US(start_tm,stop_tm) (stop_tm - start_tm)    //��΢��Ϊ��λ������ʱ�䲻׼

#define GET_CURTIME_MS(curTime)		\
{									            \
struct timeval tm;				        \
	gettimeofday(&tm, NULL);			    \
	curTime = tm.tv_sec*1000 + tm.tv_usec/1000;\
}

#else
#include <stdint.h>
#include "trace_define.h"

#define GET_CURTIME_MS(curTime)		\
{									            \
	struct timeval tm;				        \
	gettimeofday(&tm, NULL);			    \
	curTime = tm.tv_sec*1000 + tm.tv_usec/1000;\
}
typedef char  BYTE;
#define SLEEP(ms) usleep(ms*1000)
#define TIME_CNT_CURRENT(start_tm)	gettimeofday(&start_tm, NULL)
#define TIME_DIFF_MS(start_tm,stop_tm) (uint64_t)(stop_tm.tv_sec- start_tm.tv_sec)*1000+((uint64_t)stop_tm.tv_usec/1000) - ((uint64_t)start_tm.tv_usec/1000)  //�Ժ���Ϊ��λ
#define TIME_DIFF_US(start_tm,stop_tm) (stop_tm.tv_usec - start_tm.tv_usec)    //��΢��Ϊ��λ

#define mmioFOURCC( ch0, ch1, ch2, ch3 ) \
	( (uint32_t)(unsigned char)(ch0) | ( (uint32_t)(unsigned char)(ch1) << 8 ) |	\
	( (uint32_t)(unsigned char)(ch2) << 16 ) | ( (uint32_t)(unsigned char)(ch3) << 24 ) )

#endif

#ifdef _WIN32
#include "stdafx.h"
#include <io.h>
//#include "afxsock.h"
#include "winbase.h"
#include <vfw.h>
#include <assert.h>
typedef  DWORD   RESULT;
typedef  __int64  LONGLONG;
typedef  FILETIME  DVRDATETIME;
//#define  errno	GetLastError()
#define  CROSS_ASSERT(f)   assert(f)
//#define TRACE printf
#else //////Linux����
#include <sys/syscall.h>
#include <pthread.h>
#include <time.h>
#include <string.h>
#include <sys/time.h>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#define  WINAPI        *
#define  printErrorLocation(m)		{perror( m ); printf("%s():%s:%d\n", __FUNCTION__, __FILE__, __LINE__); }

typedef  timeval       DVRDATETIME;
typedef  void          RESULT;
typedef  void*         LPVOID;
typedef  unsigned long int HANDLE;
typedef  long long     LONGLONG;
typedef  RESULT WINAPI (*LPTHREAD_START_ROUTINE)(LPVOID lpParameter);
#include<assert.h>

#define CROSS_ASSERT(f)
#define _HAVE_MMC_SPT_
#define _FILE_OFFSET_BITS 64
#endif
#ifdef  _WIN32
typedef CRITICAL_SECTION  cross_lock_t;
typedef DWORD cross_time_t;
typedef  HANDLE				cross_thread_t;			//�߳�ID
typedef  LPTHREAD_START_ROUTINE	cross_start_routine;	//�̺߳���
typedef	 DWORD						cross_THREAD_RESULT;	//�̺߳������ؽ��
#define	 PUB_THREAD_CALL			WINAPI					//�������÷�ʽ

#define  CROSS_LOCK(plock)  EnterCriticalSection(plock)
#define  CROSS_UNLOCK(plock)  LeaveCriticalSection(plock)
#define STORAGE_CBK(id,param,paramLen)
#define DEVICENAME_LEN 16
#else
typedef pthread_mutex_t  cross_lock_t;
typedef struct timeval cross_time_t;
typedef  pthread_t		cross_thread_t;			        //�߳�ID
typedef  void *(*start_routine)(void*);
typedef  start_routine		cross_start_routine;		//�̺߳���
typedef	 void*					cross_THREAD_RESULT;		//�̺߳������ؽ��
#define	 PUB_THREAD_CALL									//�������÷�ʽ

#define  CROSS_LOCK(plock) pthread_mutex_lock(plock)
#define  CROSS_UNLOCK(plock) pthread_mutex_unlock(plock)
#define  DEVICENAME_LEN 12
#define  SOCKET   int
#define  INVALID_SOCKET -1
#endif

////sleep�Ժ���Ϊ��λ
inline void CrossSleep(int ms)//����
{
#ifdef _WIN32
    Sleep(ms);
#else
    usleep(ms*1000);
#endif
}

inline void Cross_InitLock(cross_lock_t *pLock)
{
#ifdef         _WIN32_
    InitializeCriticalSection(pLock);
#elif defined  __linux
    pthread_mutexattr_t mutexAttr;
    pthread_mutexattr_init(&mutexAttr);
    pthread_mutexattr_settype(&mutexAttr, PTHREAD_MUTEX_RECURSIVE);
    pthread_mutex_init(pLock, &mutexAttr);
    pthread_mutexattr_destroy(&mutexAttr);
#endif
}

inline void  Cross_DestroyLock(cross_lock_t *pLock)
{
#ifdef WIN32
    DeleteCriticalSection(pLock);
#else
    pthread_mutex_destroy(pLock);
#endif
}
//�̴߳���
cross_thread_t Cross_CreateThread(cross_start_routine start_routine, void* pParam, bool *pRun,int stackSize = SIZE_KB(512));

class CCross_Lock
{
public:
    CCross_Lock();
    ~CCross_Lock();

    void Lock();	//����
    void UnLock();	//����
#ifdef	__linux
    void CondWait();
    void CondSignal();
    int TimeLock(int iMicrosecond);	//BUGS��arm-uclibc-linux-g++�ϱ��룬ִ��������������⣬���ܳ�ʱ
    int TryLock();
#endif
private:
    cross_lock_t m_Lock;
#ifdef	__linux
    pthread_cond_t m_cond;
#endif
};

//����
#ifndef  _HAVE_COMMON_ERR_
#define _HAVE_COMMON_ERR_
enum FIFOAccessMode
{
	FIFO_ACCESS_READ,
	FIFO_ACCESS_WRITE_NONBLOCK,
	FIFO_ACCESS_WRITE_BLOCK,
};

enum ERR_TYPE
{
    //STORAGE�洢ģ�������
    //ERR_STORAGE_BEGIN   =   -10000,
    //�����ļ�����

	ERR_FIFO_OVERDATA  = -8008,
	ERR_FIFO_SIZE      = -8007,
	ERR_FIFO_NOTENOUGH = -8006,
	ERR_FIFO_NODATA    = -8005,
	ERR_FIFO_OPENED    = -8004,
	ERR_FIFO_NOOPEN	   = -8003,
	ERR_FIFO_PARAM     = -8002,
	ERR_FIFO_TIMEOUT   = -8001,
	ERR_FIFO_CREATE    = -8000,

	ERR_INDEX_NORECFILE =   -5007,  //û��������¼���ļ�
	ERR_INDEX_NOTFIT    =   -5006,	//������
	ERR_INDEX_NOTSPT    =   -5005,	//
    ERR_INDEX_OVERSEGNUM=   -5004,	//��¼�������
    ERR_INDEX_OPENED	=   -5003,	//�����ļ��Ѿ��򿪹�
    ERR_INDEX_NORECIDX	=   -5002,	//�����Ѿ��ù�
    ERR_INDEX_CHKSUM    =   -5001,	//����ļ����
    ERR_INDEX_NOINIT	=   -5000,	//����δ��ʼ��

    ////¼��,����ģ��
	ERR_FRAME_TIME            =    -4016,	//�����ʱ��
	ERR_FOR_SAVE_IDX_SPACE     =   -4015,	//������ʱ,��ν�ʡ�����ռ�
	ERR_FOR_SAVE_KEY_FRAME     =   -4014,   //�����洢I֡����,���������ǰ�л��ļ�
	ERR_NOT_KEY_FRAME_1STVEDIO =   -4013,   //��֡���ǹؼ�֡
	ERR_RESOLUTIION_SWITCH     =   -4012,   //�ֱ����л�


	ERR_DATA_AREA       =   -4011,  //�������������
	ERR_1080PTO960_MODE =   -4010,  //1080P�����л�
	ERR_FRAMETYPE_SWITCH=   -4009,  //���������л�
    ERR_NOT_RECFILE     =   -4008,
    ERR_TM_JUMP_BACK 	=   -4007,	//ʱ������, ��������Ƚ�����
    ERR_TM_JUMP_FORWARD =   -4006,	//ʱ�����䣬��ǰ����
    ERR_FILE_LIST		=   -4002,	//������ļ��б�
    ERR_SPACE_NOTENOUGH =   -4001,	//�ռ䲻����
    ERR_BAK_PATH	    =   -4000,	//����ı���·��

    //�ط�
    ERR_REPLAY_DONE     =   -3004,
    ERR_REPLAY_NODATA   =   -3003,
    ERR_REPLAY_BUFFULL  =   -3002,	//�طŻ����Ѿ����ˣ�
    ERR_REPLAY_NOFILE   =   -3001,	//û��¼���ļ�
    ERR_REPLAY_SEEK		=   -3000,	//��λ����


    //�ײ��ļ�ϵͳ
    ERR_NO_DEVICE		=   -2010,	//�豸Ϊ��
    ERR_LBA_OVERFLOW	=   -2009,  //������ַ������Ӳ�̵��������
    ERR_LBA_ACCESS_MBR	=	-2008,	//�Ƿ�����������
    ERR_NO_RECFILE		=   -2007,	//¼���ļ�ָ��Ϊ��
    ERR_ACCESS_MOD		=	-2006,	//����ķ���
    ERR_DATA_BUF_EMPYT	=	-2005,	//���ݻ���Ϊ��
    ERR_DATA_OVER_FILE	=	-2004,	//�����������ļ���С
    ERR_DATA_BIG	    =	-2003,  //����������
    ERR_DATA_SIZE       =   -2002,	//��������ݴ�0С

    //IO���̴��������
    ERR_BAD_SECTOR		=   -1005,	//����Ӳ�̻���
    ERR_HDD_HARDWARE	=   -1004,	//����Ӳ��Ӳ������
    ERR_WRITE_FAIL		=	-1003,	//дʧ��
    ERR_READ_FAIL		=	-1002,	//��дʧ��
    ERR_SEEK_FAIL		=   -1001,	//�ƶ�����ָ��ʧ��
    //STORAGE�洢ģ����������
//	ERR_STORAGE_END     =	-1000,

    //ͨ�ô�����-1�� -999
    ERR_DISK_LOST		=   -886,	//���̶�ʧ
	ERR_UNSUPPORT       =   -404,	//���ܲ�֧��
	ERR_SOCK_TIME_OUT   =   -300,//��ʱ
	ERR_SOCK_FD         =   -301,
	ERR_NO_FILE			=   -116,	//û�д��ļ�
	ERR_OVER_BUFSIZE    =   -115,	//���������С
	ERR_EMPTY_POINT     =   -114,   //��ָ��
	ERR_BUF_OPENED		=   -113,   //�����Ѿ���
	ERR_INIT_RECORDING =    -112,    //����¼����ʱ���ܹ���ʼ��
	ERR_DEL_ISWRITTING  =   -111,   //¼���ļ�����д�룬���ܹ�ɾ��
	ERR_OVER_MAX_CHANNEL=   -110,   //�����ͨ����
	ERR_OVER_MAX_TIMEMS =   -109,   //�����¼��ʱ��
	ERR_CHANNEL_NOREC   =   -108,   //ͨ��û�д�¼��
	ERR_OVER_MAXRECSEGS =   -107,   //��������¼�����
    ERR_OVER_MAXCLIENT  =   -106,
    ERR_FILE_OPENED		=   -105,	//�ļ��Ѿ���
    ERR_BIG2TB_FATPART  =   -104,	//FAT32������֧�ִ���2TB
    ERR_PARAM_NULL_POINT=   -103,	//��ָ��
    ERR_OVER_MAXDISK    =   -102,	//������������
    ERR_PARAM			=   -101,	//����Ĳ���
    ERR_FILE_NOOPEN     =   -100,	//�ļ�δ��
    ERR_FILE_OPEN	    =   -99,	//�ļ���ʧ��
    ERR_THD_RUNNING     =   -98,	//�߳���������
    ERR_FRAME_HDFLAG    =   -97,	//���������֡ͷ

    ERR_FILE_MAGIC      =   -96,	//
    NO_EER = 0,						//����û�д���
};

#endif // _HAVE_COMMON_ERR_
#define SCSI_SEEK_SET	0	/* seek relative to beginning of file */
#define SCSI_SEEK_CUR	1	/* seek relative to current file position */
#define SCSI_SEEK_END	2	/* seek relative to end of file*/

//��д��ʽ
#define	SCSI_RDONLY     0x00  //ֻ��ģʽ
#define SCSI_WRONLY	    0x01  //ֻдģʽ
#define SCSI_RDWR		0x02  //��дģʽ
#define SCSI_CLOSED	    -1	  //�ر�״̬

#define FILE_CLOSED			 -1
#define FILE_NORMAL			 0
#define FILE_WRITTING		 1
#define FILE_READING		 2
#define FILE_WRITE_FAIL		 3
#define FILE_READ_FAIL		 4
#define FILE_DEVICE_LOST	 5	//�豸�Ѿ���ʧ
//FIFO BUF
#define FIFO_BUF_READ		0
#define FIFO_BUF_WRITE		1
#define FIFO_ACCESS_DOWNLOAD 0x10
#define  CLIENT_SPT_NUM  6
#define MAX_DESC_LEN 64
#define MAX_DEVSN_LEN 24
#define MAX_REC_TAG_LEN  32
#ifndef ABS
#define ABS(x) ((x)<0 ? -(x) : (x))
#endif




//��׼Inquiry
typedef struct _ScsiCmdStdInquiryData_
{
    uchar_t          PerQualTyp; /* Peripheral Qualifier and Device Type */
    uchar_t          rmbDevType; /* Reserved except for bit 7 (RMB)      */
    uchar_t          Versions;   /* ISO, ECMA and ANSI versions          */
    uchar_t          misc;
    uchar_t          AddLengh;   /* Additional Length                    */
    uchar_t          Reserved1;  /* Reserved                             */
    uchar_t          Reserved2;  /* Reserved                             */
    uchar_t          Flags;      /* RelAdr, WBus32, WBus16, Sync, ....   */
    uchar_t          VendorId[8];/* Command Specific Information LSB     */
    uchar_t          ProdId[16]; /* Additional Sense Code                */
    uchar_t          ProdRev[4]; /* Additional Sense Code Qualifier      */
} ScsiCmdStdInquiryData;

#endif //definde _CORSS_TYPEDEF_H
